package com.hkd.sysservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkd.commonutils.R;
import com.hkd.sysservice.entity.SysUser;
import com.hkd.sysservice.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guoyu
 * @since 2020-04-08
 */
@RestController
@RequestMapping("/sysservice/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    //查询所有人员
    @GetMapping("findAll")
    public R findAllUser() {
        List<SysUser> list = sysUserService.list(null);
        return R.ok().data("item", list);
    }

    /*@GetMapping("pageUser")
    public R pageList(@RequestParam(value = "query")String queryInfo,
                      @RequestParam(value = "pagenum")int pagenum,
                      @RequestParam(value = "pagesize")int pagesize) {
        System.out.println("111");
        return R.ok();
    }*/

    //分页查询
    @GetMapping("pageUser")
    public R pageListUser(@RequestParam(value = "query") String info, @RequestParam(value = "pagenum") int current,
                            @RequestParam(value = "pagesize") int limit) {

        //创建page对象
        Page<SysUser> pageUser = new Page<>(current,limit);

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",info);
        //调用方法时，底层封装，吧分页所有数据分装到pageUser中
        sysUserService.page(pageUser, queryWrapper);

        long total = pageUser.getTotal();//总记录数
        List<SysUser> records = pageUser.getRecords();
        List<SysUser> list = sysUserService.list(null);

        Map map = new HashMap();
        map.put("total", total);
        map.put("rows",records);
        map.put("item",list);

        return R.ok().data(map);
        //第二种方式
        // return R.ok().data("total", total).data("rows",records);
    }

    //添加用户
    @PostMapping("addUser")
    public R addUser(@RequestBody SysUser sysUser){
        boolean save = sysUserService.save(sysUser);
        if (save){
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据id查询
    @GetMapping("getUser/{id}")
    public R getUser(@PathVariable String id){
        SysUser sysuser = sysUserService.getById(id);
        return R.ok().data("item", sysuser);
    }

    //用户修改
    @PostMapping("updateUser")
    public R updateUser(@RequestBody SysUser sysUser) {
        boolean flag = sysUserService.updateById(sysUser);
        if (flag){
            return R.ok();
        } else {
            return R.error();
        }
    }

    //用户删除
    @DeleteMapping("deleteUser/{id}")
    public R deleteUserByid(@PathVariable String id) {
        boolean flag = sysUserService.removeById(id);
        if (flag){
            return R.ok();
        } else {
            return R.error();
        }
    }

}

